package net.jueb.protobuf.client;

import net.jueb.protobuf.pojo.RequestProto;
import net.jueb.protobuf.pojo.RequestProto.Request;
import net.jueb.protobuf.pojo.RequestProto.Request.Builder;
import net.jueb.protobuf.pojo.ResponseProto.Response;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class ProtoClientHandler extends ChannelHandlerAdapter{
	
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		Builder builder=RequestProto.Request.newBuilder();
		builder.setMsg("Hello Server!");
		for(int i=1;i<=10;i++)
		{
			builder.setId(i);
			Request request=builder.build();
			ctx.writeAndFlush(request);
		}
	}
	
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)throws Exception {
		Response response=(Response)(msg);
		if(response==null)
		{
			return;
		}
		System.out.println("收到回复："+response.toString());
	}
	
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)throws Exception {
		cause.printStackTrace();
		ctx.close();
	}
}
